package com.lemontree;

/**
 * @Author: YLBG-YCY-1325
 * @Description:
 * @Date: 2017/8/14
 */
import java.util.Scanner;
import java.util.function.Predicate;
import java.util.regex.Pattern;

public class HotelSystem_2 {

    public static void main(String[] args) {
        Hotel ruoxiaHotel = new Hotel();
        Pattern pat = Pattern.compile("[\\s]+");
        Scanner input = new Scanner(System.in);
        do {
            String commands = input.nextLine();
            if (isBlank(commands)) {
                System.out.println("请输入正确的命令！");
                continue;
            }
            String[] command = pat.split(commands);
            switch (command[0]) {
            case "search":
                ruoxiaHotel.search(command);
                break;
            case "in":
                String customerName = command[2];
                String inRoomNumber = command[1];
                while (!ruoxiaHotel.doInRoom(inRoomNumber, customerName)) {
                    System.out.println("入住登记输入格式错误，请重新输入【房间号】+【空格】+【用户名】完成入住登记，exit退出【登记操作】");
                    commands = input.nextLine();
                    command = pat.split(commands);

                    if ("EXIT".equals(command[0].toUpperCase())) {
                        System.out.println("您已经退出入住登记...");
                        break;
                    } else if (command.length == 2) {
                        inRoomNumber = command[0];
                        customerName = command[1];
                    }
                }
                break;
            case "out":
                System.out.println();
                String numOrName = command[1];
                while (!ruoxiaHotel.doOutRoom(numOrName)) {
                    System.out.println("退房输入格式错误,请重新输入【房间号】进行退房操作，exit退出【退房操作】");
                    commands = input.nextLine();
                    command = pat.split(commands);
                    if ("EXIT".equals(command[0].toUpperCase())) {
                        System.out.println("您已经退出退房操作...");
                        break;
                    } else if (command.length == 2) {
                        numOrName = command[0];
                    }
                }
                break;
            case "quit":
                System.exit(0);
                break;
            case "help":
                ruoxiaHotel.prompt(); // 所有hotel用同一个help
                break;

            default:
                System.out.println("请输入正确格式！！");

            }
        } while (true);

    }

    static boolean isBlank(String str) {
        if (null == str || str.length() <= 0) {
            return true;
        }
        return false;
    }
}

class Hotel implements Command, Help {

    private static final String SEPARATOR = "/";

    private static final String EMPTY_NAME = "空";

    private static final String EMPTY_STRING = "";

    private static final String BLANK_STRING = "  ";

    private static int rcol = 10;

    private static int roomNums = 30;

    private static String WELCOME = "欢迎来到霞若酒店，请问你需要什么服务？>>"; // 默认欢迎语句

    private static String COMMAND_DETAIL = "search-查房,in-入住,out-退房,quit-退出系统,help-帮助";

    private Room[] rooms; // 一个酒店对应不同的房间数组，面向对象思想

    // 默认初始化酒店房间
    Hotel() {
        this(roomNums, rcol, WELCOME, COMMAND_DETAIL);
    }

    // 自定义酒店包括自定义房间多少
    Hotel(int roomNums, int rcol, String welcome, String commands) {
        rooms = initiaRoom(roomNums, rcol);
        if (rooms.length <= 0) {
            System.out.println("room init fail!");
            return;
        }
        System.out.println(welcome);
        System.out.println(commands);
        System.out.println();
        System.out.println("房间信息如下：");
        this.showRoom(item -> true);
    }

    // init room
    Room[] initiaRoom(int roomNums, int rcol) {
        rooms = new Room[roomNums];
        int row = 1;
        int col = 1;
        for (int i = 0; i < roomNums; i++) {
            if (col == rcol) {
                row++;
                col = 1;
            }
            Room room = new Room(EMPTY_STRING, String.valueOf(row * 100 + col), Boolean.TRUE);
            rooms[i] = room;
            col++;
        }
        return rooms;
    }

    boolean showRoom(Predicate<Room> predicate) {
        boolean flag = false;
        int col = 1;
        for (int i = 0; i < rooms.length; i++) {
            if (col == rcol) {
                System.out.println();
                col = 1;
            }
            if (predicate.test(rooms[i])) {
                System.out.print(rooms[i].getRoomNo() + SEPARATOR
                        + (rooms[i].isEmpty() ? EMPTY_NAME : rooms[i].getCustomerName())
                        + BLANK_STRING);
                col++;
                flag = true;
            }
        }
        System.out.println();
        return flag;
    }

    boolean doOutRoom(String roomNumber) {
        return inOrOutRoom(roomNumber, room -> {
            if (room.isEmpty()) {
                System.out.println("该房间已经是空房间,请勿重复操作");
                return false;
            }
            room.setEmpty(Boolean.TRUE);
            room.setCustomerName(EMPTY_STRING);
            System.out.println("退房成功~");
            return true;
        });
    }

    boolean doInRoom(String roomNumber, String customerName) {
        return inOrOutRoom(roomNumber, room -> {
            if (!room.isEmpty()) {
                System.out.println("该房间已入驻客户！");
                return false;
            }
            room.setEmpty(Boolean.FALSE);
            room.setCustomerName(customerName);
            System.out.println("入驻成功~");
            return true;
        });
    }

    boolean inOrOutRoom(String roomNumber, Predicate<Room> predicate) {
        if (null == roomNumber || roomNumber.length() <= 0) {
            System.out.println("请输入正确的房间号");
            return false;
        }
        for (int i = 0; i < rooms.length; i++) {
            if (rooms[i].getRoomNo().equals(roomNumber)) {
                return predicate.test(rooms[i]);
            }
        }
        System.out.println("请输入正确的房间号");
        return false;
    }

    @Override
    public void search(String[] commands) {
        if (commands.length == 1) {
            if (!this.showRoom(item -> true)) {
                System.out.println("获取不到房间信息");
            }
        } else {
            String numOrName = commands[1];
            if (!this.showRoom((item) -> item.getRoomNo().equals(numOrName)
                    || item.getCustomerName().equals(numOrName))) {
                System.out.println("根据房间号或用户名查询不到对应房间信息");
            }
        }
    }

    @Override
    public void prompt() {
        System.out.println("【search】 ------------查找所有房间信息");
        System.out.println("【search】+【空格】+【房间号或用户名】 --------------查找所有房间信息");
        System.out.println("【in】+【空格】+【房间号】+【空格】+用户名  ------------------  入住");
        System.out.println("out+【空格】+【房间号】  -------------------退房");
        System.out.println("quit   -----------------退出系统");
        System.out.println("help    ---------------------帮助");
        System.out.println("如需详情，请在help后跟具体指令");
    }
}

class Room {
    private String customerName;

    private String roomNo;

    private boolean isEmpty; // true is empty; false is full;

    public Room(String customerName, String roomNo, boolean roomStatus) {
        this.customerName = customerName;
        this.roomNo = roomNo;
        this.isEmpty = roomStatus;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getRoomNo() {
        return roomNo;
    }

    public void setRoomNo(String roomNo) {
        this.roomNo = roomNo;
    }

    public boolean isEmpty() {
        return isEmpty;
    }

    public void setEmpty(boolean empty) {
        isEmpty = empty;
    }
}

interface Command {
    void search(String[] commands);
}

interface Help {
    void prompt();
}
